package com.vitas.coin.ui.act

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.BarUtils
import com.google.gson.Gson
import com.snap.common.main.BaseMVVMAct
import com.snap.util.edge.marginTopSystemWindowInsets
import com.snap.util.edge.paddingBottomSystemWindowInsets
import com.snap.util.runUIThread
import com.snap.util.toast
import com.vitas.coin.R
import com.vitas.coin.data.bean.VideoBean
import com.vitas.coin.databinding.ActMakeBinding
import com.vitas.coin.util.Http
import com.vitas.coin.util.Utils
import com.vitas.coin.vm.MakeVM
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.Collections


class MakeAct : BaseMVVMAct<ActMakeBinding, MakeVM>() {

    companion object {
        const val PARENT_ID = "PARENT_ID"
    }


    override fun createVM(): MakeVM {
        return MakeVM().apply {
            actionAct = { this@MakeAct }
        }
    }

    override fun doDataBind() {
        binding.viewModel = viewModel
        binding.page = this
    }

    override fun getContentViewId(): Int {
        return R.layout.act_make
    }

    override fun onCreate() {
        binding.clContent.paddingBottomSystemWindowInsets()
        binding.rlTop.marginTopSystemWindowInsets()
        // BarUtils.addMarginTopEqualStatusBarHeight(binding.rlTop)
        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, true)

        initParentId()
        initObserve()
    }

    private fun initParentId() {
        viewModel.initParentId(intent.getLongExtra(PARENT_ID, 0L))
    }


    private fun initObserve() {
//        BaseUtil.observer(this, ChooseEvent::class.java) {
//            viewModel.addMusic(it.musicBean)
//        }
    }

    fun onClickBack(view: View) {
        finish()
    }

    fun onClickClear(view: View) {
        binding.et.setText("")
    }

    fun onClickTake(view: View) {
        getUrl()
    }

    private fun getUrl() {
//        if (true) {
//            val url =
//                "https://v11.douyinvod.com/588896d89d97fb715918cd9e41831931/6916e10c/video/tos/cn/tos-cn-ve-15/oIMA2gFAXQDa6ygAaQwgfuEupDIf95oACydP2B/?a=1128&br=1401&bt=1401&btag=c0000e000ad000&cc=33&cd=0%7C0%7C0%7C0&ch=0&cquery=100y&cr=0&cs=0&cv=1&dr=0&ds=4&dy_q=1763103336&dy_va_biz_cert=&er=0&feature_id=0ea98fd3bdc3c6c14a3d0804cc272721&ft=raapL00712Nx~rxPqUxBtcKlHC~YwrDhDIniXwG&l=20251114145536C129D12F0820674FDAD4&mime_type=video_mp4&qs=0&rc=OmlpZWc0O2RoMzo3ZTtnN0BpM2Vrb3M5cmhpNzMzNGkzM0AuMDIzYWNhNWIxNV4uYGA2YSNoam5fMmRrYS1hLS1kLS9zcw%3D%3D&req_cdn_type="
//            var i = Intent(this@MakeAct, TakeAct::class.java)
//            i.putExtra("path", url)
//            startActivity(i)
//            return
//        }
        if (binding.bar.visibility === View.VISIBLE) {
            return
        }
        Utils.hideInput(binding.frame)
        var url = binding.et.getText().toString().trim()
        if (TextUtils.isEmpty(url)) {
            toast("请输入视频链接")
            return
        }
        var i = url.indexOf("http://")
        if (i == -1) {
            i = url.indexOf("https://")
        }
        if (i >= 0) {
            url = url.substring(i)
        } else {
            toast("输入内容无效")
            return
        }
        i = url.indexOf(" ")
        if (i > 0) {
            url = url.substring(0, i)
        }
        binding.bar.visibility = View.VISIBLE
        getUrl(url)
    }

    fun getUrl(url: String) {
        GlobalScope.launch {
            var param = HashMap<String, String>()
            param.put("url", url)
            try {
                var str = Http.get(
                    "https://jk.y-l.cc/api/dsp/3240EB6120B97E82CEDAA757A6D8CC1A17C46F076ED9714079/202037242/",
                    param, Collections.emptyMap<String, String>()
                )
                var bean = Gson().fromJson<VideoBean>(str, VideoBean::class.java)
                if (bean.code == 200) {
                    runUIThread {
                        binding.bar.visibility = View.GONE
                        if (bean.data.down == null || bean.data.down.isEmpty()) {
                            toast("该视频仅能在线观看")
                            return@runUIThread
                        }
                        toast("解析成功")
                        var i = Intent(this@MakeAct, TakeAct::class.java)
                        i.putExtra("path", bean.data.url)
                        startActivity(i)
                    }
                } else {
                    runUIThread {
                        binding.bar.visibility = View.GONE
                        toast(bean.msg)
                    }
                }
            } catch (e: Throwable) {
                e.printStackTrace()
                runUIThread {
                    binding.bar.visibility = View.GONE
                    toast("解析失败")
                }
            }
        }
    }
}